// pages/mine/mine.js
import {
  common_kefy_image
} from '../../libs/resource'
import {configData} from '../../data/config.js'

Page({
  data: {
    common_kefy_image,
    avatarUrl: './user-unlogin.png',
    userInfo: {},
    mine_list: []
  },
  onLoad: function (options) {
    this.get_mine_data()
  },
  onShow: function (params) {
    var userInfo = getApp().globalData.userInfo
    this.setData({
      avatarUrl: userInfo.avatarUrl,
      userInfo: userInfo
    })
  },
  onPullDownRefresh: function () {

  },
  // 获取个人信息=> 跳转访客界面
  getUserInfoJump(e) {
    this.getUserInfo(e, true)
  },
  // 获取个人信息
  getUserInfo(e, jumpToVisitor = false) {
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          console.log("已授权=====")
          wx.getUserInfo({
            success: res => {
              console.log("获取用户信息成功", res)
              this.updateUserInfo(res.userInfo)
              this.setData({
                avatarUrl: res.userInfo.avatarUrl,
                userInfo: res.userInfo
              })
              // 如果需要跳转就执行下面的代码
              if (jumpToVisitor)
                wx.navigateTo({
                  url: '../visitors/visitors',
                })
            },
            fail: err => {
              console.log("获取用户信息失败", err)
            }
          })
        } else {
          console.log("未授权=====")
          if (!jumpToVisitor)
            this.showSettingToast("请授权")
        }
      }
    })
  },
  // 打开权限设置页提示框
  showSettingToast: function (e) {
    wx.showModal({
      title: '提示！',
      content: "未授权将无法获取您的头像信息",
      confirmText: '去授权',
      showCancel: true,
      success: function (res) {
        if (res.confirm) {
          wx.openSetting()
        }
      }
    })
  },
  // 更新用户信息
  updateUserInfo(userInfo) {
    //设置全局的用户信息
    getApp().globalData.userInfo = userInfo
    wx.cloud.callFunction({
      name: "config",
      data: {
        action: 'updateUserInfo'
      }
    }).then(res => {
      console.log("updateUserInfo", res)
    }).catch(err => {
      console.error(err)
    })
  },
  // 获取配置数据
  get_mine_data: function () {
    let mGrids = configData.filter(item => item.flag == 'mine')
    this.add_collection_db({data: mGrids, openid: 'oC_xc5SmwHu-C-Lw_pS8Jv_hWmVk'})
  },
  // 添加我的数据库到集合里面
  add_collection_db(mineData) {
    var app = getApp()
    var mineList = mineData.data
    var openid = mineData.openid
    var myOpenid = app.globalData.myOpenid
    var collcetion = {
      icon: 'folder',
      iconClass: 'fa-file',
      title: '我的数据库',
      url: 'collection/collection'
    }
    console.log(openid, myOpenid)
    if (openid == myOpenid)
      mineList.push(collcetion)
    console.log(mineList)
    this.setData({
      mine_list: mineList
    })
  },
  // 列表击事件
  _onItemClick(event) {
    console.log(event)
    var navi_url = event.currentTarget.dataset.url;
    if (navi_url === '') {
      wx.showToast({
        icon: 'none',
        title: '正在开发中！'
      })
    } else {
      wx.navigateTo({
        url: navi_url
      })
    }
  },
  testClick() {
    wx.cloud.callFunction({
      name: 'wxPower',
      data: {
        siteSearch: '开发工具'
      }
    }).then(res => {
      console.log(res)
    })
  },
  testClick1() {
    wx.startSoterAuthentication({
      requestAuthModes: ['fingerPrint'],
      challenge: '123456',
      authContent: '请用指纹解锁',
      success(res) {
        console.log("testClick", res)
      }
    })
  }
})
